package com.example.guarding_jc;

public class ChallengeCommand {
	private byte[] challenge;
	private byte[] IMEI;
	private byte[] time;
	
	public ChallengeCommand(byte[] IMEI, byte[] time){
		this.IMEI = IMEI.clone();
		this.time = time.clone();
		this.challenge = null;
	}
	
	public ChallengeCommand(byte[] challenge, byte[] IMEI, byte[] time){
		this.IMEI = IMEI.clone();
		this.time = time.clone();
		this.challenge = challenge.clone();
	}
	
	public byte[] toByteArray(){
		int challengeLen = 0;
		if (challenge != null)
			challengeLen = challenge.length;
		int IMEILen = IMEI.length;
		int timeLen = time.length;
		
		byte[] cmdApdu = new byte[challengeLen + IMEILen + timeLen + 6];
		cmdApdu[0] = (byte)0x00;
		cmdApdu[1] = (byte)0x11;
		cmdApdu[2] = (byte)0x03; //this is the number of fields
		
		cmdApdu[3] = (byte)challengeLen;
		cmdApdu[4] = (byte)IMEILen;
		cmdApdu[5] = (byte)timeLen;
		if (challenge != null)
			System.arraycopy(challenge, 0, cmdApdu, 6, challengeLen);
		System.arraycopy(IMEI, 0, cmdApdu, 6 + challengeLen, IMEILen);
		System.arraycopy(time, 0, cmdApdu, 6 + challengeLen + IMEILen, timeLen);
		
		return cmdApdu;
	}
	
	public String toString(){
		String sChallenge = Utility.toHexString(challenge);
		String sIMEI = Utility.toHexString(IMEI);
		String sTime = Utility.toHexString(time);
		
		return sIMEI + "--" + sChallenge + "--" + sTime;
	}
}
